﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace VirtualControl
{
    public class VirtualButton : VirtualBase
    {
        public Color MouseLeaveColor = Color.Red;
        public Color MouseEnterColor = Color.Yellow;
        public Color MouseDownColor = Color.Blue;
        public Color CheckedColor = Color.Lime;

        public bool CheckedEnabled = false;
        public bool ParentMouseLeaveHide = false;
        public bool ParentMouseLeaveCheckedShow = false;

        bool isChecked = false;
        public bool Checked
        {
            get
            {
                if (CheckedBinding != null) this.isChecked = CheckedBinding();

                return this.isChecked;
            }
            set
            {
                if (CheckedBinding == null) this.isChecked = value;
            }
        }

        public delegate bool CheckedCallback();
        /// <summary>
        /// 绑定外部状态
        /// </summary>
        public CheckedCallback CheckedBinding;

        public Size ParentMinSize { get; set; }

        public VirtualButton(Control parent)
            : base(parent)
        {

        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (this.CheckedEnabled) this.Checked = !this.Checked;

            base.OnMouseClick(e);
        }

        protected override void OnPaint(Graphics G)
        {
            if (this.CheckedEnabled && this.Checked)
                this.ForeColor = this.CheckedColor;
            else if (this.isMouseDown)
                this.ForeColor = this.MouseDownColor;
            else if (this.isMouseEnter)
                this.ForeColor = this.MouseEnterColor;
            else
                this.ForeColor = this.MouseLeaveColor;

            if (this.Parent.Width > this.ParentMinSize.Width 
                && this.Parent.Height > this.ParentMinSize.Height
                && (this.ParentMouseLeaveHide && this.isParentMouseEnter 
                    || !this.ParentMouseLeaveHide 
                    || this.ParentMouseLeaveCheckedShow && this.CheckedEnabled && this.Checked))
            {
                base.OnPaint(G);
            }
        }
    }
}
